# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/7/10 21:17'

'''
selenium浏览器的有无头
'''

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from time import sleep


def driver_headless(type="chrome", headless=False,driver_path="chromedriver.exe"):
    '''
    创建浏览器驱动（支持有头和无头）
    :param type:  浏览器类别
    :param headless: True -- 无头拉起浏览器（后台执行）,False：有头（前台拉起浏览器执行）
    :param driver_path: 驱动路径,此处我把geckodriver.exe放到虚拟环境下面后，可以不加driver_path
    :return: driver
    '''
    driver = None
    if type == "firefox":
        FirefoxOpt = webdriver.FirefoxOptions()
        if headless:
            FirefoxOpt.add_argument("--headless")  #设置firefox浏览器为无头
        #设置UA代理选项
        FirefoxOpt.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
        driver = webdriver.Firefox(executable_path=driver_path, firefox_options=FirefoxOpt)
    elif type == "ie":
        ie_opt = webdriver.IeOptions()
        if headless:
            ie_opt.add_argument("--headless")
        driver = webdriver.Ie(executable_path=driver_path, ie_options=ie_opt)
    elif type == "chrome":
        chrome_opt =  webdriver.ChromeOptions()
        if headless:
            chrome_opt.add_argument("--headless")
        driver = webdriver.Chrome(executable_path=driver_path,options=chrome_opt)
    return driver


